<?php

require_once('classes/modeles/Application/Profil.php');

/**
 * Description of Application
 *
 * @author ROCU Fabien
 */
class Application {

    public $array_profil_id;
    public $array_profil;

    /**************************************************************************/
    /*************************** PROFIL ***************************************/
    /**************************************************************************/

    public function charger_arrayProfilID_application() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT profil_id FROM profil";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['profil_id'];

                }

                $this->array_profil_id = $tmp;
            }
            else
                throw new Exception('Erreur Application: la création de la liste des identifiants de profil a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayProfil_application() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_profil_id) ; $i++) {
            $profil = new Profil();
            $profil->charger_profil($this->array_profil_id[$i]);

            $tmp[$this->array_profil_id[$i]] = serialize($profil);
        }

        $this->array_profil = $tmp;
    }

    public function creer_profil( $nom, $droit ) {
        $profil = new Profil();
        $profil->set_profil($nom, $droit);
        $id = $profil->creer_profil();

        $nb_profil = count($this->array_profil_id);
        $this->array_profil_id[$nb_profil] = $id;

        $this->array_profil[$id] = serialize($profil);
    }

    public function sauvegarder_profil( $id, $nom, $droit ) {
        $profil = unserialize($this->array_profil[$id]);
        $profil->set_profil($nom, $droit);
        $profil->sauvegarder_profil();

        $this->array_profil[$id] = serialize($profil);
    }

    public function effacer_profil( $id ) {
        $profil = new Profil();
        $profil->effacer_profil($id);

        unset($this->array_profil[$id]);

        $this->charger_arrayProfilID_application();
    }

}
?>
